export function timeFix() {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function idCard(UUserCard, num) {
  if (num === 1) {
    var birth = UUserCard.substring(6, 10) + '-' + UUserCard.substring(10, 12) + '-' + UUserCard.substring(12, 14)
    return birth
  }
  if (num === 2) {
    if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
      return '1'
    } else {
      return '0'
    }
  }
  if (num === 3) {
    var myDate = new Date()
    var month = myDate.getMonth() + 1
    var day = myDate.getDate()
    var age = myDate.getFullYear() - UUserCard.substring(6, 10)
    // if (
    //   UUserCard.substring(10, 12) < month ||
    //   (UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day)
    // ) {
    //   age++
    // }
    return age
  }
}
export function addWater(name) {
  console.log(document)
  const container = document.getElementsByClassName('waterArea')
  const width = '300px'
  const height = '150px'
  const textAlign = 'center'
  const textBaseline = 'middle'
  const font = '20px Microsoft Yahei'
  const fillStyle = 'rgba(230, 230, 230, 0.8)'
  const content = name
  const rotate = '10'
  const zIndex = 0
  var args = arguments[0]
  var canvas = document.createElement('canvas')

  canvas.setAttribute('width', width)
  canvas.setAttribute('height', height)
  var ctx = canvas.getContext('2d')

  ctx.textAlign = textAlign
  ctx.textBaseline = textBaseline
  ctx.font = font
  ctx.fillStyle = fillStyle
  ctx.rotate((-20 * Math.PI) / 180)
  ctx.fillText(content, parseFloat(width) / 2, parseFloat(height) / 2)

  var base64Url = canvas.toDataURL()
  const watermarkDiv = document.createElement('div')
  watermarkDiv.setAttribute(
    'style',
    `
        position:absolute;
        top:0;
        left:0;
        width:100%;
        height:100%;
        z-index:${zIndex};
        pointer-events:none;
        background-repeat:repeat;
        background-image:url('${base64Url}')`
  )
  watermarkDiv.classList.add('waterm')
  if (container && container.length > 0) {
    container.forEach(element => {
      if (element.firstChild.className.indexOf('waterm') == -1) {
        // element.style.position = 'relative'
        element.insertBefore(watermarkDiv, element.firstChild)
      }
    })
  }

}
export function deleteChildrenIfNull(a) {
  if (a && a.children == null) {
    delete a.children
  } else if (a && a.children.length === 0) {
    delete a.children
  }
}

/** 两个数组合并成一个数组对象
    * @param array1== id array2 == name */
export function mergeArraysIntoObjects(array1, array2) {
  return array1.map((item, index) => ({ value: item, label: array2[index] }));
}

export function deleteChildrenIfNull2(a) {
  if (a && a.Childrens == null) {
    delete a.Childrens
  } else if (a && a.Childrens.length === 0) {
    delete a.Childrens
  }
}
export function tranShortDate(a) {
  if (a == null || a === undefined) {
    return ''
  } else {
    var date = a.replace(/T/g, ' ').replace(/.[\d]{3}Z/, ' ')
    return date.substring(0, 7)
  }
}
export function tranDate(a) {
  if (a == null || a === undefined) {
    return ''
  } else {
    var date = a.replace(/T/g, ' ').replace(/.[\d]{3}Z/, ' ')
    return date.substring(0, 10)
  }
}
export function tranDateDetail(a) {
  if (a == null || a === undefined) {
    return ''
  } else {
    var date = a.replace(/T/g, ' ').replace(/.[\d]{3}Z/, ' ')
    return date.substring(0, 16)
  }
}
/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent() {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader(callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () { }
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE() {
  const bw = window.navigator.userAgent
  const compare = s => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate(id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}

export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    return value.toString().padStart(2, '0')
  })
  return time_str
}
